package com.example.springbootmybatis.web;

import com.example.springbootmybatis.entity.Area;
import com.example.springbootmybatis.service.AreaService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/v1/area")
@Api(value = "区域操作",tags = {"TiDB测试接口"})
public class AreaController {

    @Autowired
    private AreaService areaService;


    @ApiOperation(value = "查询所有区域", notes = "查询所有区域")
    @GetMapping("list")
    private Map<String, Object> list(){
        List<Area> areas = areaService.findall();
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("areas", areas);
        return res;
    }
    @ApiOperation(value = "根据Id查询区域", notes = "根据Id查询区域")
    @GetMapping("listbyid")
    private Map<String, Object> listbyid(@RequestParam int id){
        Area area = areaService.findById(id);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("area", area);
        return res;
    }
    @ApiOperation(value = "添加区域", notes = "添加区域")
    @PostMapping("add")
    private Map<String, Object> add(@RequestBody Area o){
        boolean area = areaService.save(o);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("success", area);
        return res;
    }
    @ApiOperation(value = "更新区域", notes = "更新区域")
    @PostMapping("upd")
    private Map<String, Object> upd(@RequestBody Area o){
        boolean area = areaService.edit(o);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("success", area);
        return res;
    }
    @ApiOperation(value = "删除区域", notes = "删除区域")
    @GetMapping(value = "del")
    private Map<String, Object> del(@RequestParam int id){
        boolean area = areaService.del(id);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("success", area);
        return res;
    }


}
